﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Espam.Idiomas.Decimo.Tesis.Negocio;
using Microsoft.Reporting.WebForms;

namespace Espam.Idiomas.Decimo.Tesis.Web
{
    public partial class DownloadSchedulePerTeacher : System.Web.UI.Page
    {
        string validaSession = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
                Response.Cache.SetAllowResponseInBrowserHistory(false);
                Response.Cache.SetNoStore();

                if (Session["userEspamIdiomasVarSession001"] == null || Session["rolEspamIdiomasVarSession003"] == null)
                {
                    validaSession = "NoHaIniciadoSession";
                    Response.Redirect("UI_Login.aspx?validaSession=" + validaSession);
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Docente")
                {
                    CargaGridViewHorarios();
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Administradores")
                {
                    Response.Redirect("Default.aspx");
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Alumno")
                {
                    Response.Redirect("UI_DashboardStudents.aspx");
                }
            }
        }
       
        private void CargaGridViewHorarios()
        {
            ListadoHorarios oListadoHorarios=new ListadoHorarios();
            List<Horarios> lista = new List<Horarios>();
            oListadoHorarios.idDocente = (int)Session["idDocenteEspamIdiomasVarSession009"];
            lista = oListadoHorarios.GetHorariosByDocente();
            if (lista.Count > 0)
            {
                gvListaHorarios.DataSource = lista;
                gvListaHorarios.DataBind();
                lblMensajeErrorListaHorarios.Text = "";
                idMensajeListas.Visible = false; ;
                filaAceptar.Visible = true; ;
            }
            else
            {
                lblMensajeErrorListaHorarios.Text = "SIN MÓDULOS ASIGNADOS HASTA EL MOMENTO";
                idMensajeListas.Visible = true;
                filaAceptar.Visible = false;
            }
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("UI_DashboardTeacher.aspx");
        }

        protected void btnImprimirListaHorarios_Click(object sender, EventArgs e)
        {
            CrearReporte();
        }

        private void CrearReporte()
        {
            int idDocente = (int)Session["idDocenteEspamIdiomasVarSession009"];
            EspamIdiomasDataSet.sp_getHorariosByDocenteDataTable dt = new EspamIdiomasDataSet.sp_getHorariosByDocenteDataTable();
            EspamIdiomasDataSetTableAdapters.sp_getHorariosByDocenteTableAdapter da = new EspamIdiomasDataSetTableAdapters.sp_getHorariosByDocenteTableAdapter();
            da.Fill(dt, idDocente);

            ReportViewer viewer = new ReportViewer();
            ReportDataSource RD = new ReportDataSource();
            RD.Value = dt;
            RD.Name = "DataSet1";
            viewer.LocalReport.DataSources.Clear();
            viewer.LocalReport.DataSources.Add(RD);
            viewer.LocalReport.ReportEmbeddedResource = "ListadoEstudiantes.rdlc";
            viewer.LocalReport.ReportPath = Server.MapPath("Reports/Teachers/ListadoHorarios.rdlc");
            viewer.LocalReport.Refresh();

            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;
            byte[] pdfContent = viewer.LocalReport.Render("PDF", string.Empty, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            this.Response.Clear();
            this.Response.ContentType = "application/pdf";
            this.Response.AddHeader("Content-disposition", "attachment; filename=ListadoHorarios.pdf");
            this.Response.BinaryWrite(pdfContent);
            this.Response.End();
        }
    }
}